package com.sp.scmdb.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.AreaBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.Area;
import com.sp.scmdb.entity.LabelReadingMachine;
import com.sp.scmdb.entity.Map;
import com.sp.scmdb.service.AreaService;
import com.sp.scmdb.service.MapService;
import com.sp.scmdb.service.ReaderService;

@Controller("areaAction")
@Scope("prototype")
public class AreaAction extends BaseAction<AreaBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(AreaAction.class);
	private AreaService areaService;
	private ReaderService readerService;
	private MapService mapService;

	@Autowired
	public void setMapService(MapService mapService) {
		this.mapService = mapService;
	}

	@Autowired
	public void setReaderService(ReaderService readerService) {
		this.readerService = readerService;
	}

	@Autowired
	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	@SuppressWarnings("unchecked")
	public String findAll() {
		try {
			AreaBean areaBean = null;
			if (StringUtils.isNotEmpty(pageName)) {
				areaBean = (AreaBean) session.get("areaBean");
			}else{
				entity.setPageNo(1);
			}
			setEntity(areaService.queryAll(entity, areaBean));
			session.put("areaBean", entity);
			session.put("areaListType", CommonParamter.TypeCommon.getAreaType());
			this.button("Super.Popup(5,'添加','/SCMDB/areaAction!newArea.action','',420,300)",
					"Super.editData('/SCMDB/areaAction!findArea.action?id=',420,300)",
					"Super.delData('/SCMDB/areaAction!delArea.action?pId=')");
			return SUCCESS;
		} catch (Exception e) {
			log.error("get area failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	@SuppressWarnings("unchecked")
	public String findArea() {
		try {
			String pId = request.getParameter("id");
			List<LabelReadingMachine> readList = readerService.queryAll();
			session.put("readLists", readList);
			session.put("areaListType", CommonParamter.TypeCommon.getAreaType());
			entity = areaService.queryAreaByID(pId);
			return "editSucc";
		} catch (Exception e) {
			log.error("edit area failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	public String editArea() {
		try {
			UserBean user = (UserBean) session.get("user");
			String result = areaService.editArea(entity, user);
			messageSession(result, "区域");
			return "commitSucc";
		} catch (Exception e) {
			log.error("edit reader failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	public String addArea() {
		try {
			UserBean user = (UserBean) session.get("user");
			String result = areaService.newArea(entity, user);
			messageSession(result, "读卡器");
			
			return "commitSucc";
		} catch (Exception e) {
			log.error("add area failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	@SuppressWarnings("unchecked")
	public String newArea() {
		List<LabelReadingMachine> readList = readerService.queryAll();
		session.put("readLists", readList);
		session.put("areaListType", CommonParamter.TypeCommon.getAreaType());
		return "newSucc";
	}

	@SuppressWarnings("unchecked")
	public String initArea() {
		try {
			List<LabelReadingMachine> listRead = readerService.queryAll();
			request.setAttribute("listRead", listRead);
			List<Map> listMap = mapService.queryIsUsed();
			request.setAttribute("listMap", listMap);
			List<Area> listArea = areaService.queryAllArea();
			request.setAttribute("listArea", listArea);
			session.put("areaListType", CommonParamter.TypeCommon.getAreaType());
			return "setSucc";
		} catch (Exception e) {
			log.error("init reader failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	public String resetArea() {
		try {
			UserBean user = (UserBean) session.get("user");
			areaService.editAreaPosition(entity, user);
			return SUCCESS;
		} catch (Exception e) {
			log.error("reset reader failed! reason:" + e.getMessage());
			return SUCCESS;
		}
	}

	public String delArea() {
		String flg = null;
		try {
			UserBean user = (UserBean) session.get("user");
			String pId = request.getParameter("pId");
			if (StringUtils.isNotEmpty(pId)) {
				String[] arr = pId.split(",");
				flg = areaService.delArea(arr, user);
			}
			if ("10".equals(flg)) {
				messageSession("0", "该区域关联了特种人员，不能删除！");	
			} else {
				messageSession(flg, "读卡器");
				log.info("delete suncce");
			}
		} catch (Exception e) {
			log.error("delete fail" + e.getMessage());
		}
		return "commitSucc";
	}
}
